package com.dream.app.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dream.app.cache.AppPositionCache;
import com.dream.app.service.AppPositionService;
import com.dream.dao.base.AppPositionDAO;
import com.dream.model.app.AppAbstractUser;
import com.dream.model.app.AppPosition;
import com.dream.vo.app.PositionVO;

@Service("appPositionService")
public class AppPositionServiceImpl implements AppPositionService {
	
	@Autowired
	private AppPositionDAO appPositionDAO;
	
	@Autowired
	private AppPositionCache positionCache;
	
	@Override
	@Transactional
	public List<AppPosition> list() {
		return appPositionDAO.selectAll(false);
	}

	@Override
	@Transactional
	public void addNew(PositionVO vo, AppAbstractUser user) {
		
		AppPosition po = new AppPosition();
		vo.populate(po);
		po.setCreateTime(new Date());
		po.setCreateUser(user.getId());
		appPositionDAO.save(po);
		positionCache.reload();
		
	}

	@Override
	@Transactional
	public void delete(Long id, AppAbstractUser user) {
			
		AppPosition pos = appPositionDAO.selectById(id);
		pos.setUpdateTime(new Date());
		pos.setUpdateUser(user.getId());
		pos.setDeleted(true);
		appPositionDAO.saveOrUpdate(pos);
		positionCache.reload();
	}

}
